package com.party.admin.web.controller.counterfoil;

import com.google.common.collect.Maps;
import com.party.admin.web.dto.AjaxResult;
import com.party.common.constant.Constant;
import com.party.common.utils.StringUtils;
import com.party.core.dao.write.member.MemberActWriteDao;
import com.party.core.model.YesNoStatus;
import com.party.core.model.activity.ActStatus;
import com.party.core.model.activity.Activity;
import com.party.core.model.counterfoil.Counterfoil;
import com.party.core.model.counterfoil.CounterfoilBusiness;
import com.party.core.model.counterfoil.CounterfoilType;
import com.party.core.model.member.MemberAct;
import com.party.core.model.order.OrderForm;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.counterfoil.ICounterfoilBusinessService;
import com.party.core.service.counterfoil.ICounterfoilService;
import com.party.core.service.member.IMemberActService;
import com.party.core.service.order.IOrderFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 票据
 *
 * @author Administrator
 */
@Controller
@RequestMapping(value = "/counterfoil/counterfoil")
public class CounterfoilController {

    @Autowired
    IActivityService activityService;
    @Autowired
    ICounterfoilService counterfoilService;
    @Autowired
    ICounterfoilBusinessService counterfoilBusinessService;
    @Autowired
    IMemberActService memberActService;
    @Autowired
    IOrderFormService orderFormService;
    @Autowired
    MemberActWriteDao memberActWriteDao;

    /**
     * 迁移票据数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateCounterfoil")
    public AjaxResult updateCounterfoil() {
        Activity aa = new Activity();
        aa.setIsCrowdfunded(YesNoStatus.NO.getCode());
        List<Activity> activities = activityService.list(aa);
        for (Activity activity : activities) {
            Float price = activity.getPrice();
            String name = "";
            if (price.equals(Float.valueOf("0.0"))) {
                name = "免费票";
            } else {
                name = price + "元票";
            }
            Counterfoil search = new Counterfoil();
            search.setName(name);
            search.setBusinessId(activity.getId());
            search.setBusinessType(Constant.BUSINESS_TYPE_ACTIVITY);
            Counterfoil counterfoil = counterfoilService.getUnique(search);
            if (counterfoil == null) {
                counterfoil = new Counterfoil();
            }
            counterfoil.setPayment(price);
            counterfoil.setLimitNum(getLimitNum(activity.getLimitNum()));
            counterfoil.setBusinessId(activity.getId());
            counterfoil.setMaxBuyNum(1);
            counterfoil.setBusinessType(Constant.BUSINESS_TYPE_ACTIVITY);
            counterfoil.setName(name);
            counterfoil.setJoinNum(0);

            if (StringUtils.isNotEmpty(counterfoil.getId())) {
                counterfoilService.update(counterfoil);
            } else {
                counterfoilService.insert(counterfoil);
            }
        }
        return AjaxResult.success();
    }

    public Integer getLimitNum(Integer limitNum) {
        if (limitNum == null) {
            return 1000;
        } else if (limitNum != null && limitNum.equals(Integer.valueOf(0))) {
            return 1000;
        } else {
            return limitNum;
        }
    }

    /**
     * 迁移票据购买数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateCounterfoilBusiness")
    public AjaxResult updateCounterfoilBusiness() {
        Activity aa = new Activity();
        aa.setIsCrowdfunded(YesNoStatus.NO.getCode());
        List<Activity> activities = activityService.list(aa);
        for (Activity activity : activities) {
            MemberAct mm = new MemberAct();
            mm.setActId(activity.getId());
            List<MemberAct> memberActs = memberActService.list(mm);
            for (MemberAct memberAct : memberActs) {
                Counterfoil search = new Counterfoil();
                search.setBusinessId(memberAct.getActId());
                search.setBusinessType(Constant.BUSINESS_TYPE_ACTIVITY);

                Float payment = memberAct.getPayment();
                if (StringUtils.isNotEmpty(memberAct.getOrderId())) {
                    OrderForm orderForm = orderFormService.get(memberAct.getOrderId());
                    if (orderForm != null) {
                        payment = orderForm.getPayment();
                    }
                }
                String name = "";
                if (payment.equals(Float.valueOf("0.0"))) {
                    name = "免费票";
                } else {
                    name = payment + "元票";
                }
                search.setName(name);
                Counterfoil counterfoil = counterfoilService.getUnique(search);
                if (counterfoil == null) {
                    counterfoil = new Counterfoil();
                    counterfoil.setPayment(payment);
                    counterfoil.setLimitNum(getLimitNum(activity.getLimitNum()));
                    counterfoil.setBusinessId(memberAct.getActId());
                    counterfoil.setMaxBuyNum(1);
                    counterfoil.setBusinessType(Constant.BUSINESS_TYPE_ACTIVITY);
                    counterfoil.setName(name);
                    counterfoil.setJoinNum(0);
                    counterfoilService.insert(counterfoil);
                }

                CounterfoilBusiness business = counterfoilBusinessService.getUnique(new CounterfoilBusiness(counterfoil.getId(), memberAct.getId()));
                if (business == null) {
                    business = new CounterfoilBusiness();
                    business.setCounterfoilId(counterfoil.getId());
                    business.setBusinessId(memberAct.getId());
                    business.setCreateBy(memberAct.getMemberId());
                    counterfoilBusinessService.insert(business);
                }
            }
        }
        return AjaxResult.success();
    }

    /**
     * 更新票据购买人数
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("updateJoinNum")
    public AjaxResult updateJoinNum() {
        List<Counterfoil> counterfoils = counterfoilService.list(new Counterfoil());
        for (Counterfoil cc : counterfoils) {
            Map<String, Object> params = Maps.newHashMap();
            params.put("actId", cc.getBusinessId());
            params.put("counterfoilId", cc.getId());
            Integer joinNumber = memberActService.getSuccessMemberAct(params);
            Counterfoil dbCounterfoil = counterfoilService.get(cc.getId());
            dbCounterfoil.setJoinNum(joinNumber);
            counterfoilService.update(dbCounterfoil);
        }
        return AjaxResult.success();
    }

    /**
     * 更新票据名称
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("updateCounterfoilName")
    public AjaxResult updateCounterfoilName() {
        Activity aa = new Activity();
        aa.setIsCrowdfunded(YesNoStatus.NO.getCode());
        List<Activity> activities = activityService.list(aa);
        for (Activity activity : activities) {
            String actId = activity.getId();
            MemberAct mm = new MemberAct();
            mm.setActId(actId);
            List<Counterfoil> counterfoils = counterfoilService.list(new Counterfoil(actId, Constant.BUSINESS_TYPE_ACTIVITY));
            List<MemberAct> memberActs = memberActService.list(mm);
            for (MemberAct memberAct : memberActs) {
                MemberAct dbMemberAct = memberActService.get(memberAct.getId());
                if (StringUtils.isNotEmpty(dbMemberAct.getCounterfoilId())) {
                    for (Counterfoil c : counterfoils) {
                        if (dbMemberAct.getCounterfoilId().equals(c.getId())) {
                            dbMemberAct.setCounterfoilName(c.getName());
                            memberActWriteDao.update(dbMemberAct);
                            break;
                        }
                    }
                }
            }
        }
        return AjaxResult.success();
    }

    /**
     * 更新票据类型
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("updateCounterfoilType")
    public AjaxResult updateCounterfoilType() {
        // 更新票据类型
        List<Counterfoil> counterfoils = counterfoilService.list(new Counterfoil());
        for (Counterfoil cc : counterfoils) {
            if (cc.getPayment() > Float.valueOf("0.0")) {
                cc.setType(CounterfoilType.TYPE_ON_LINE.getCode());
            } else {
                cc.setType(CounterfoilType.TYPE_FREE.getCode());
            }
            counterfoilService.update(cc);
        }

        Activity aa = new Activity();
        aa.setIsCrowdfunded(YesNoStatus.NO.getCode());
        List<Activity> activities = activityService.list(aa);
        for (Activity activity : activities) {
            String actId = activity.getId();
            MemberAct mm = new MemberAct();
            mm.setActId(actId);
            List<Counterfoil> counterfoils2 = counterfoilService.list(new Counterfoil(actId, Constant.BUSINESS_TYPE_ACTIVITY));
            List<MemberAct> memberActs = memberActService.list(mm);
            for (MemberAct memberAct : memberActs) {
                MemberAct dbMemberAct = memberActService.get(memberAct.getId());
                // 更新票据类型
                Integer counterType = null;
                if (StringUtils.isNotEmpty(dbMemberAct.getCounterfoilId())) {
                    for (Counterfoil c : counterfoils2) {
                        if (dbMemberAct.getCounterfoilId().equals(c.getId())) {
                            counterType = c.getType();
                            break;
                        }
                    }
                }
                if (counterType == null) {
                    if (dbMemberAct.getPayment().equals(Float.valueOf("0.0"))) {
                        counterType = CounterfoilType.TYPE_FREE.getCode();
                    } else {
                        counterType = CounterfoilType.TYPE_ON_LINE.getCode();
                    }
                }
                dbMemberAct.setCounterfoilType(counterType);

                // 更新支付状态
                if (null != dbMemberAct.getCheckStatus()) {
                    if (dbMemberAct.getCheckStatus().equals(ActStatus.ACT_STATUS_PAID.getCode())) {
                        dbMemberAct.setPayStatus(MemberAct.PAY_STATUS_YES);
                    } else {
                        dbMemberAct.setPayStatus(MemberAct.PAY_STATUS_NO);
                    }
                }
                memberActWriteDao.update(dbMemberAct);
            }
        }
        return AjaxResult.success();
    }
}
